import numpy as np
import random as rd
import drawNet as dn
def BA(n, m0, m):#无标度网络
    G = np.empty((n, n))
    deg = np.zeros((n))
    for i in range(1, m0):
        G[i - 1][i] = G[i][i - 1] = 1
        deg[i] += 1
        deg[i - 1] += 1
    for i in range(m0, n):
        cnt = 0
        prep = vis = np.zeros((n))
        p = deg 
        sump = np.sum(p)
        p = p / sump#优先连接
        prep[0] = 0
        for j in range(1, n):
            prep[i] = prep[i - 1] + p[i]
        while(cnt < m):#连m条边
            r = rd.random()
            for j in range(1, n):
                if(r > prep[j]):
                    if(vis[j - 1] == 0):
                        G[i][j - 1] = G[j - 1][i] = 1
                        vis[j - 1] = 1
                        cnt += 1
                        break
        deg[i] = m
        deg = deg + vis#更新入度
    return G
dn.drawGraph(BA(10, 3, 2))